#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

#
# Build U-Boot image when `mkimage' tool is available.
#

# We need access to CONFIG_ symbols
case "${KCONFIG_CONFIG}" in
*/*)
	. "${KCONFIG_CONFIG}"
	;;
*)
	# Force using a file from the current directory
	. "./${KCONFIG_CONFIG}"
esac

if [ -n "${CONFIG_RTOS_MKHEADER_UIMAGE}" ]; then
	mkimage="mkheader"
else
	mkimage="mkimage"
fi

MKIMAGE=$(type -path "${CROSS_COMPILE}${mkimage}")

if [ -z "${MKIMAGE}" ]; then
	MKIMAGE=$(type -path ${mkimage})
	if [ -z "${MKIMAGE}" ]; then
		# Doesn't exist
		echo "${mkimage} command not found - U-Boot images will not be built" >&2
		exit 1;
	fi
fi

# Call "mkimage" to create U-Boot image
${MKIMAGE} "$@"
